//
//  DYMSDingzuoViewController.swift
//  SheYangBigData
//
//  Created by 叁拾叁 on 2021/2/5.
//  Copyright © 2021 叁拾叁. All rights reserved.
//

import UIKit
import SwiftDate

class DYMSDingzuoViewController: SYBaseViewController {
    
    var isDefalutSec: Bool = true
    var isDefalutFour: Bool = true
    
    let types = ["大厅", "包间"]
    
    var showStr: String!
    
    var fir: DYMSDingzuoView!
    
    var sec: DYMSDingzuoView!
    
    var thir: DYMSDingzuoView!
    
    var four: DYMSDingzuoView!
    
    var bottom: DYMSDingzuoBottom!
    
    var id: String!
    
    var params: [String: String] = [:]
    var timeParams: [String: String] = [:]
    var orderParams: [String: String] = [:] {
        didSet {
            self.configureBottomView()
        }
    }
    
    var isYudings: [Bool] = []
    var isSecYudings: [Bool] = []
    
    var infoModel: DYMSYudingInfoModel!
    
    init(_ id: String, _ title: String) {
        super.init(nibName: nil, bundle: nil)
        self.id = id
        // isPrivate 是否是包间 0不是 1是
        self.params = ["restaurantId":self.id, "isPrivate": "0"]
        self.timeParams = ["restaurantId":self.id]
        self.title = title
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        getData {[weak self] in
            if let `self` = self {
                var defaultIndex = 0
                for (i, value) in self.isYudings.enumerated() {
                    if value {
                        defaultIndex = i
                        break
                    }
                }
                var defaultSecIndex = 0
                for (i, value) in self.isSecYudings.enumerated() {
                    if value {
                        defaultSecIndex = i
                        break
                    }
                }
                self.fir.collectionView(self.fir.collectionView, didSelectItemAt: IndexPath(item: defaultIndex, section: 0))
                self.sec.collectionView(self.sec.collectionView, didSelectItemAt: IndexPath(item: defaultSecIndex, section: 0))
                self.thir.collectionView(self.thir.collectionView, didSelectItemAt: IndexPath(item: 0, section: 0))
                self.four.collectionView(self.four.collectionView, didSelectItemAt: IndexPath(item: 0, section: 0))
            }
        }
        initViews()
    }
    
    func initViews() {
        fir = DYMSDingzuoView(frame: CGRect(x: 15.autoWidth(), y: 20.autoWidth() + NAV_HEIGHT, width: SCREEN_WIDTH-15.autoWidth(), height: 46.autoWidth()))
        fir.clickedClosure = {[weak self] index in
            if let `self` = self {
//                if true == self.infoModel.deskVoList?[index].can {
                    
                    self.orderParams["deskId"] = self.infoModel.deskVoList?[index].id
                    self.orderParams["renshu"] = String(format: "%ld", self.infoModel.deskVoList?[index].suitableNum ?? 0)
//                } else {
//                    self.sy_toast("您已经预订了此桌")
//                }
            }
        }
        fir.currentVC = self
        view.addSubview(fir)
        sec = DYMSDingzuoView(frame: CGRect(x: 15.autoWidth(), y: 94.autoWidth() + NAV_HEIGHT, width: SCREEN_WIDTH-15.autoWidth(), height: 46.autoWidth()))
        sec.clickedClosure = {[weak self] index in
            if let `self` = self {
//                if true == self.infoModel.openVoList?[index].can {
                    self.timeParams["dayTime"] = self.infoModel.openVoList?[index].t ?? ""
                    self.params["dayTime"] = self.infoModel.openVoList?[index].t ?? ""
                    self.orderParams["riqi"] = self.infoModel.openVoList?[index].t ?? ""
                    self.orderParams["day"] = self.infoModel.openVoList?[index].tstr ?? ""
                    if !self.isDefalutSec {
                        self.getTime()
                    }
                    self.isDefalutSec = false
//                } else {
//                    self.sy_toast("您该日期已经有预订")
//                }
            }
        }
        sec.currentVC = self
        view.addSubview(sec)
        thir = DYMSDingzuoView(frame: CGRect(x: 15.autoWidth(), y: 170.autoWidth() + NAV_HEIGHT, width: SCREEN_WIDTH-15.autoWidth(), height: 46.autoWidth()))
        thir.clickedClosure = {[weak self] index in
            if let `self` = self {
                self.orderParams["shijian"] = self.infoModel.hourList?[index] ?? ""
            }
        }
        view.addSubview(thir)
        four = DYMSDingzuoView(frame: CGRect(x: 15.autoWidth(), y: 244.autoWidth() + NAV_HEIGHT, width: SCREEN_WIDTH-15.autoWidth(), height: 46.autoWidth()))
        four.clickedClosure = {[weak self] index in
            if let `self` = self {
                self.params["isPrivate"] = "\(index)"
                self.orderParams["isPrivate"] = "\(index)"
                if !self.isDefalutFour {
                    self.getData(success:nil)
                }
                self.isDefalutFour = false
            }
        }
        
        self.four.configureView(types)
        
        view.addSubview(four)
        bottom = DYMSDingzuoBottom(frame: CGRect(x: 0, y: SCREEN_HEIGHT - BOTTOM_SAFE_HEIGHT - 56.autoWidth(), width: SCREEN_WIDTH, height: 56.autoWidth()))
        bottom.currentVC = self
        bottom.clickedClosure = {[weak self] _ in
            if let `self` = self {
                self.orderParams["showStr"] = self.showStr
                self.orderParams["time"] = (self.orderParams["riqi"] ?? "") + " " + (self.orderParams["shijian"] ?? "")
                let vc = DYMSDingzuorenViewController(params: self.orderParams, title: self.title ?? "")
                self.sy_push(vc)
            }
        }
        view.addSubview(bottom)
    }
    
    func configureBottomView() {
        
        var riqi: String = ""
        if let d = self.orderParams["riqi"] {
            let date = DateInRegion(d)
            riqi = "\(date?.month ?? 0)月\(date?.day ?? 0)日"
        }
        var tip: String = ""
        if (self.orderParams["day"] ?? "").checkIsChinese() {
            tip = self.orderParams["day"] ?? ""
        }
        let index: Int = Int(self.orderParams["isPrivate"] ?? "0") ?? 0
        
        showStr = "\(riqi) \(tip)  \(self.orderParams["shijian"] ?? "")，\(self.orderParams["renshu"] ?? "")位，\(types[index])"
        
        self.bottom.configure(showStr)
    }
    
    func getData(success: (()->())?) {
        requestVM.baseRequest(disposeBag: dispose, type: .dy_meishiYudingInfo(dic: self.params), modelClass: SGBaseModel<DYMSYudingInfoModel>.self) {[weak self] (res) in
            if let `self` = self {
                self.infoModel = res.data
                let desks = self.infoModel.deskVoList?.map { "\($0.suitableNum)位" } ?? []
                self.isYudings = self.infoModel.deskVoList?.map { $0.can } ?? []
                self.fir.configureView(desks, self.isYudings)
                let days = self.infoModel.openVoList?.map { $0.tstr ?? "" } ?? []
                self.isSecYudings = self.infoModel.openVoList?.map { $0.can } ?? []
                self.sec.configureView(days, self.isSecYudings)
                let times = self.infoModel.hourList ?? []
                self.thir.configureView(times)
                success?()
            }
            
        } Error: {
            
        }
    }
    
    func getTime() {
        requestVM.baseRequest(disposeBag: dispose, type: .dy_meishiYudingChooseTime(dic: self.timeParams), modelClass: SGBaseModel<DYMSDetailModel>.self) {[weak self] (res) in
            if let `self` = self {
                let times = self.infoModel.hourList ?? []
                self.thir.configureView(times)
            }
            
        } Error: {
            
        }
    }
    
}

